<?php
use tpfcore\Core;
?>
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <!-- Set render engine for 360 browser -->
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- HTML5 shim for IE8 support of HTML5 elements -->
    <!--[if lt IE 9]>
		<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js">
		</script>
	<![endif]-->
    <link href="{$admin_assets_path}/css/theme.min.css" rel="stylesheet">
    <link href="{$admin_assets_path}/css/simplebootadmin.css" rel="stylesheet">
    <link href="{$admin_assets_path}/css/default.css" rel="stylesheet" />
    <link href="{$admin_assets_path}/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <style>
    .length_3 {
        width: 180px;
    }

    form .input-order {
        margin-bottom: 0px;
        padding: 3px;
        width: 40px;
    }

    .table-actions {
        margin-top: 5px;
        margin-bottom: 5px;
        padding: 0px;
    }

    .table-list {
        margin-bottom: 0px;
    }
    .table-label label{display: inline-block;}
    .attribute{margin-top: 10px;}
    </style>
    <!--[if IE 7]>
		<link rel="stylesheet" href="{$admin_assets_path}/css/font-awesome-ie7.min.css">
	<![endif]-->
    <script type="text/javascript">
    //全局变量
    var GV = {
        WEB_ROOT: "",
        DIMAUB: "/",
        JS_ROOT: "/theme/backend/assets/js/",
        TOKEN: ""
    };
    </script>
    <!-- Le javascript==================================================-
	-> <!-- Placed at the end of the document so the pages load faster -->
    <script src="{$admin_assets_path}/js/jquery.js"></script>
    <script src="{$admin_assets_path}/js/wind.js"></script>
    <script src="{$admin_assets_path}/js/bootstrap.min.js"></script>
    <link href="{$admin_assets_path}/kindeditor/themes/default/default.css" rel="stylesheet" />
    <script src="{$admin_assets_path}/kindeditor/kindeditor-min.js"></script>
    <script>
    var editor;
    KindEditor.ready(function(K) {
        var uploadbutton = K.uploadbutton({
            button: K('#uploadButton')[0],
            fieldName: 'imgFile',
            url: "{:url('mall/GoodsCategory/upload')}",
            afterUpload: function(data) {
                if (data.code === 0) {
                    var url = K.formatUrl(data.data[0], 'absolute');
                    K('#ico').val(url);
                    $('#show_thumb').attr('src', url).show();
                } else {
                    alert(data.msg);
                }
            },
            afterError: function(str) {
                alert('错误信息已打印到控制台');
                console.log(str);
            }
        });
        uploadbutton.fileBox.change(function(e) {
            uploadbutton.submit();
        });
    });
    </script>
</head>

<body>
    <div class="wrap">
        <ul class="nav nav-tabs">
            <li><a href="{:url('mall/GoodsCategory/index')}">分类列表</a></li>
            {empty name="list"}
            <li class="active"><a href="{:url('mall/GoodsCategory/add')}">添加分类</a></li>
            {else/}
            <li><a href="{:url('mall/GoodsCategory/add')}">添加分类</a></li>
            <li class="active"><a href="{:url('mall/GoodsCategory/add',['id'=>$list[0]->id])}">编辑分类</a></li>
            {/empty}
        </ul>
        <form method="post" class="form-horizontal js-ajax-form" action="{:url('mall/GoodsCategory/add')}">
        	{notempty name="list"}
        	<input type="hidden" name="id" value="{$list[0]->id}" />
        	{/notempty}
            <fieldset>
                <div class="">
                    <table class="table table-bordered">
                        <tbody>
                            <tr>
                                <th width="80">分类名称</th>
                                <td>
                                    <input type="text" name="name" value="{$list[0]->name|default=''}" placeholder="请输入分类名称"> <span class="form-required">*</span>
                                </td>
                            </tr>
                            <tr>
                                <th width="80">上级分类</th>
                                <td>
                                    <select name="parent_id">
                                        <option value="0">顶级分类</option>
                                        {$categorys|default=''} 
                                    </select>
                                    
                                </td>
                            </tr>
                            <tr>
                                <th width="80">排序</th>
                                <td class="table-label">
                                    <input type="text" value="0" name="sort" />
                                </td>
                            </tr>
                            <tr>
                                <th width="80">是否显示</th>
                                <td class="table-label">
                                    {empty name="list"}
                                    <label><input type="radio" name="display" value="1" checked="checked"/> 显示</label>
                                    <label><input type="radio" name="display" value="0"> 不显示</label>
                                    {else/}
                                    <label><input type="radio" name="display" value="1" {eq name="list[0]->display" value="1"}checked="checked"{/eq}/> 显示</label>
                                    <label><input type="radio" name="display" value="0" {eq name="list[0]->display" value="0"}checked="checked"{/eq}/> 不显示</label>
                                    {/empty}
                                </td>
                            </tr>
                            <tr>
                                <th width="80">品牌图标/图片</th>
                                <td>
                                    <input class="ke-input-text" type="text" id="ico" value="{$list[0]->ico|default=''}" name='ico' readonly="readonly" /> 
                                    <input type="button" id="uploadButton" value="上传缩略图" />
                                    {empty name="list"}
                                    <img src="" style="width:50px; height: auto;display:none;" id="show_thumb"/>
                                    {else/}
                                        {empty name="list[0]->ico"}
                                        <img src="" style="width:50px; height: auto;display:none;" id="show_thumb"/>
                                        {else/}
                                        <img src="{$list[0]->ico}" style="width:50px; height: auto;" id="show_thumb"/>
                                        {/empty}
                                    {/empty}
                                </td>
                            </tr>
                            <tr>
                                <th>筛选属性</th>
                                <td>
                                    {empty name="list"}
                                    <div class="attribute">
                                        <a href="javascript:;" id="addAttribute">[+]</a>
                                        <select onchange="changeAttribute(this)">
                                            <option value="0">请选择商品类型</option>
                                            {volist name="listGoodsType" id="vo"}
                                            <option value="{$vo.id}">{$vo.name}</option>
                                            {/volist}
                                        </select>
                                        <select name="filter_attr[]">
                                            <option value="0">请选择筛选属性</option>
                                        </select>
                                    </div>
                                    {else/}
                                    {empty name="filter_attr_list"}
                                    <div class="attribute">
                                        <a href="javascript:;" id="addAttribute">[+]</a>
                                        <select onchange="changeAttribute(this)">
                                            <option value="0">请选择商品类型</option>
                                            {volist name="listGoodsType" id="vo"}
                                            <option value="{$vo.id}">{$vo.name}</option>
                                            {/volist}
                                        </select>
                                        <select name="filter_attr[]">
                                            <option value="0">请选择筛选属性</option>
                                        </select>
                                    </div>
                                    {else/}
                                    {volist name="filter_attr_list" id="fal"}
                                    <div class="attribute">
                                        {if condition="$key eq 0"}
                                        <a href="javascript:;" id="addAttribute">[+]</a>
                                        {else/}
                                        <a href="javascript:;" class="remoteAttribute">[-]&nbsp;</a>
                                        {/if}
                                        <select onchange="changeAttribute(this)">
                                            <option value="0">请选择商品类型</option>
                                            {volist name="listGoodsType" id="vo"}
                                            <option value="{$vo.id}" {eq name="vo.id" value="$fal.type_id"}selected="selected"{/eq}>{$vo.name}</option>
                                            {/volist}
                                        </select>
                                        <select name="filter_attr[]">
                                            <option value="0">请选择筛选属性</option>
                                            {volist name="fal.goods_type_list" id="gtl"}
                                            <option value="{$key}" {eq name="key" value="$fal.filter_attr"}selected="selectd"{/eq}>{$gtl}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                    {/volist}
                                    {/empty}
                                    {/empty}
                                </td>
                            </tr>
                            <tr>
                                <th width="80">分类关键词</th>
                                <td class="table-label">
                                    <input type="text" name="keywords" value="{$list[0]->keywords|default=''}" placeholder="分类关键词" style="width: 700px;" />
                                </td>
                            </tr>
                            <tr>
                                <th>分类描述</th>
                                <td>
                                    <textarea style="width: 300px; height: 120px;" name="desc" placeholder="分类描述">{$list[0]->desc|default=''} </textarea>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <div class="form-actions">
                <button class="btn btn-primary js-ajax-button" type="button">确定操作</button>
                <a class="btn" href="javascript:history.back(-1);">返回</a>
            </div>
        </form>
    </div>
    <script src="{$admin_assets_path}/js/common.js"></script>
    <script type="text/javascript">
    remoteAttribute();
    var arr = new Array();
    {volist name="listGoodsType" id="gt"}
    arr[{$gt.id}]=new Array();
        {volist name="listGoodsAttribute" id="ga"}
        {eq name="gt.id" value="$ga.type_id"}
        arr[{$gt.id}].push(["{$ga.id}","{$ga.name}"]);
        {/eq}
        {/volist}
    {/volist}

    function changeAttribute(obj){
        obj=$(obj),val = obj.val();
        var selectObj = obj.parents(".attribute").find("select:eq(1)");
        selectObj.html("<option value='0'>请选择筛选属性</option");
        if(val == 0) return ;
        for(o in arr[val]){
            selectObj.append('<option value="'+arr[val][o][0]+'">'+arr[val][o][1]+'</option');
        }
    }

    $("#addAttribute").click(function(){
        var disabled=false;
        $(".attribute").each(function(i){
            if($(this).find("select:eq(1)").val()==0){
                Wind.use('artDialog', 'iframeTools', function() {
                    art.dialog({   
                        title:"消息提醒",
                        content: "请选择筛选属性后再添加新的",
                        icon: 'warning',
                        ok: function () {   
                            return true;   
                        }
                    }); 
                });
                disabled = true;
            }
        });
        if(disabled) return false;
        var clone = $(this).parents(".attribute").clone();
        clone.find("select:eq(1)").html("<option value='0'>请选择筛选属性</option");
        clone.html(clone.html().replace(/id="addAttribute"(.*)(\[)(\+])/i, "class='remoteAttribute'$1$2-]&nbsp;"));
        $(this).parents(".attribute").parent().append(clone);
        remoteAttribute();
    });

    function remoteAttribute(){
        $(".remoteAttribute").click(function(){
            $(this).parents(".attribute").remove();
        });
    }
    </script>
</body>

</html>